using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class admin_frmUsersPasswordEdit : System.Web.UI.Page
{
    clsUser clsUser;
    clsUsersRecord clsUsersRecord;

    #region EVENT MODULES

    protected void Page_Load(object sender, EventArgs e)
    {
        //### Check if session clsUser exists
        if (Session["clsUser"] == null)
        {
            //### Redirect back to login
            Response.Redirect("../frmAdminLogin.aspx");
        }

        clsUser = (clsUser)Session["clsUser"];

        if (!IsPostBack)
        {
            //### If the UserID is passed through then we want to instantiate the obect with that iUsrID
            if (Request.QueryString["iUserID"] != "" && Request.QueryString["iUserID"] != null)
            {
                //## Set label to modify
                lblAddModify.Text = "Modify Password";

                clsUsersRecord = new clsUsersRecord(clsUser, Convert.ToInt32(Request.QueryString["iUserID"]));

            }
            Session["clsUsersRecord"] = clsUsersRecord;
        }
        else
        {
            clsUsersRecord = (clsUsersRecord)Session["clsUsersRecord"];
        }
    }

    protected void btnBack_Click(object sender, ImageClickEventArgs e)
    {
        //### Go back to previous page
        Response.Redirect("frmUsersView.aspx");
    }

    protected void btnSave_Click(object sender, ImageClickEventArgs e)
    {
        //### Validate registration process
        bool bCanSave = true;

        if (txtPassword.Text == "")
        {
            bCanSave = false;
            reqPassword.Attributes.Add("style", "display:block");
            txtPassword.Attributes.Add("style", "border:1px solid #911f22;");
        }
        else
        {
            reqPassword.Attributes.Add("style", "display:none");
            txtPassword.Attributes.Add("style", "border:1px solid #999;");
        }

        if (txtConfirmPassword.Text == "")
        {
            bCanSave = false;
            reqConfirmPassword.Attributes.Add("style", "display:block");
            txtConfirmPassword.Attributes.Add("style", "border:1px solid #911f22;");
        }
        else
        {
            reqConfirmPassword.Attributes.Add("style", "display:none");
            txtConfirmPassword.Attributes.Add("style", "border:1px solid #999;");
        }

        if (bCanSave == true)
        {
            SaveData();
        }
        else
        {
            reqContainerDiv.Visible = true;
        }
    }

    protected void btnClear_Click(object sender, ImageClickEventArgs e)
    {
        reqContainerDiv.Visible = false;

        txtPassword.Text = "";
        txtPassword.Attributes.Add("style", "border:1px solid #999;");

        txtConfirmPassword.Text = "";
        txtConfirmPassword.Attributes.Add("style", "border:1px solid #999;");

    }

    #endregion

    #region POPULATE DATA METHODS

    #endregion

    #region SAVE DATA METHODS

    private void SaveData()
    {
        //### Hash the password
        string strHashPassword = clsCommonFunctions.GetMd5Sum(txtPassword.Text);

        //### Only update the password if there is a value
        if (txtPassword.Text != "")
        {
            clsUsersRecord.strPassword = strHashPassword;
        }

        clsUsersRecord.Update();

        //### redirect back to view page
        Response.Redirect("frmUsersView.aspx");

    }

    #endregion

}
